#include <linux/module.h>
#include <linux/init.h>
#include <linux/percpu.h>
#include <linux/cpumask.h>

static DEFINE_PER_CPU(long, cpuvar)=5;

static long __percpu *cpualloc;

static int __init percpu_init(void)
{
    int cpu;
    pr_info("Module loaded at %p\n", percpu_init);
    for_each_possible_cpu(cpu)
    {
        per_cpu(cpuvar, cpu) = 10;
        pr_info("Init Cpuvar on cpu%d=%ld\n", cpu, get_cpu_var(cpuvar));
        put_cpu_var(cpuvar);
    }

    __this_cpu_write(cpuvar, 10);
    cpualloc=alloc_percpu(long);
    for_each_possible_cpu(cpu)
    {
        *per_cpu_ptr(cpualloc, cpu) = 6688;
        pr_info("Init cpu:%d *cpualloc=%ld\n", cpu, *per_cpu_ptr(cpualloc, cpu));
    }
    return 0;
}

static void __exit percpu_exit(void)
{
    int cpu;
    pr_info("Exit Module.....\n");
    for_each_possible_cpu(cpu)
    {
        pr_info("Cpuvar CPU %d=%ld\n", cpu, per_cpu(cpuvar, cpu));
        pr_info("Exit:Cpualloc %d=%lf\n", cpu, *per_cpu_ptr(cpualloc));
    }
    free_percpu(cpualloc);
    pr_info("End : Module Unloaded from 0x%p\n", percpu_exit);
}

module_init(percpu_init);
module_exit(percpu_exit);

MODULE_AUTHOR("zhuguoyin");
MODULE_LICENSE("GPL");